[2025-07-15] Command-injection-1

๐Ÿฆฅ ๋ณธ๋ฌธ

  • app.py
#!/usr/bin/env python3
import subprocess

from flask import Flask, request, render_template, redirect

from flag import FLAG

APP = Flask(__name__)

@APP.route('/')
def index():
    return render_template('index.html')

@APP.route('/ping', methods=['GET', 'POST'])
def ping():
    if request.method == 'POST':
        host = request.form.get('host')
        cmd = f'ping -c 3 "{host}"'
        try:
            output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
            return render_template('ping_result.html', data=output.decode('utf-8'))
        except subprocess.TimeoutExpired:
            return render_template('ping_result.html', data='Timeout !')
        except subprocess.CalledProcessError:
            return render_template('ping_result.html', data=f'an error occurred while executing the command. -> {cmd}')

    return render_template('ping.html')

if __name__ == '__main__':
    APP.run(host='0.0.0.0', port=8000)

/ping API์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋‚ธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ…์ŠคํŠธํ™” ํ•˜์—ฌ ping -c 3 ์„ ์‹คํ–‰ํ•œ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— โ€œ๋ฅผ ํ†ตํ•ด ํ…์ŠคํŠธํ™”๋ฅผ ๋ฒ—์–ด๋‚˜ flag.py๋ฅผ cat์„ ํ†ตํ•ด ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ

  • ping.html
<input type="text" class="form-control" id="Host" placeholder="8.8.8.8" name="host" pattern="[A-Za-z0-9.]{5,20}" required>

์œ„์™€ ๊ฐ™์€ ํ•„ํ„ฐ๋ง์ด ์žˆ๋Š” ๋ฐ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ํ†ตํ•ด ์ง€์›Œ์ค€ ํ›„ ๋ณด๋‚ธ๋‹ค

ํ’€์ด

  1. ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ํ†ตํ•ด pattern ๋ถ€๋ถ„์„ ์ง€์šด๋‹ค.
  2. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋ณด๋‚ธ๋‹ค
8.8.8.8"; cat ./flag.py #

Categories:

Updated:

Leave a comment